<?php

require_once('functions/pageLoad.php');
loginRequired();
$user_session = explode("_", $_SESSION['user']);
$user_id = $user_session[0];

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['tid']) && isset($_POST['jid'])){

if($_POST['jid'] == '0'){
header("Location: /post-a-job?custom=".$_POST['tid']);
}

else{

$sql = "SELECT id FROM job_to_tradesman WHERE tradesman_id = '".mysql_real_escape_string($_POST['tid'])."' AND job_id = '".mysql_real_escape_string($_POST['jid'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$rows=mysql_num_rows($query);

if($rows > 0){
$content = '<p>A request has been already been sent to that tradesman.</p>';
}

else{

$sql = "INSERT INTO job_to_tradesman (job_id, tradesman_id) VALUES ('".mysql_real_escape_string($_POST['jid'])."', '".mysql_real_escape_string($_POST['tid'])."')";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

$sql = "SELECT company_name, email, mobile, firstname, surname, mobile_verified, custom_quotes FROM tradesman 	
	LEFT JOIN users ON tradesman.user_id = users.id 
	WHERE tradesman.id = '".mysql_real_escape_string($_POST['tid'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$user_rs=mysql_fetch_assoc($query);

$sql = "SELECT trade, area FROM jobs 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	WHERE jobs.id = '".mysql_real_escape_string($_POST['jid'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$job_rs=mysql_fetch_assoc($query);

$content = '<p>'.ucfirst($user_rs['company_name']).' has been notified of your request.</p>';

// send email

$job_link = build_job_link($job_rs['trade'],$job_rs['area'],$_POST['jid']);

require_once("classes/class.phpmailer.php");
	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = "mail@iwantatradesman.co.uk";
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($user_rs['email']);

	$mail->Subject = "Quote request";
	$mail->Body = "Dear ".$user_rs['firstname']." ".$user_rs['surname']."\r\n\r\nA user has requested that you provide a quote for their job.\r\n\r\nYou can view the job here: http://www.iwantatradesman.co.uk".$job_link."\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

// send SMS?

$sql = "SELECT sms_remaining FROM SMS WHERE tradesman_id = '".mysql_real_escape_string($_POST['tid'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$sms_rs=mysql_fetch_assoc($query);

if($sms_rs['sms_remaining'] > 0 && $user_rs['mobile_verified'] == '1' && $user_rs['custom_quotes'] == '1'){

// SEND SMS

	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['sms_email'];
	$mail->FromName = $settings['sms_email'];

	$mail->AddAddress("44".substr($user_rs['mobile'], 1)."@bulksms.co.uk");

	$mail->Subject = $settings['sms_password'];
	$mail->Body = "You have received a quote request on I Want A Tradesman. Go to your account now and provide a quotation.";

	$mail->Send();

// deduct sms

$new_sms_remaining = $sms_rs['sms_remaining'] - 1;

$sql = "UPDATE SMS SET sms_remaining = '".$new_sms_remaining."' WHERE tradesman_id = '".mysql_real_escape_string($_POST['tid'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

}

}

}

}

else{

if(isset($_GET['tid'])){

$sql = "SELECT jobs.id, jobs.title FROM jobs 
	LEFT JOIN tradesman_to_trade ON jobs.trade_id = tradesman_to_trade.trade_id 
	WHERE jobs.user_id = '".$user_id."' AND tradesman_to_trade.tradesman_id = '".mysql_real_escape_string($_GET['tid'])."' AND jobs.job_active = '1'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$rows = mysql_num_rows($query);

if($rows > 0){

while($rs=mysql_fetch_assoc($query)){

$jobs_content.= '<br /><input type="radio" name="jid" id="job_'.$rs['id'].'" value="'.$rs['id'].'" /> <label for="job_'.$rs['id'].'">'.ucfirst($rs['title']).'</label>';

}

}

else{

header("Location: /post-a-job?custom=".$_GET['tid']);

}

$content = '<form method="post" action="/request_quote.php">
<input type="hidden" name="tid" value="'.$_GET['tid'].'" />
<p>Please select the job you would like to add the tradesman to;</p>
<p style="line-height: 2;"><input type="radio" name="jid" id="job_0" value="0" checked="checked" /> <label for="job_0">Post a new job</label>
'.$jobs_content.'</p>
<br /><input type="image" src="/images/submit_button.gif" alt="Submit" />
</form>
';

}

}

$page_title = 'Request a quote';

include('includes/meta.php');
include('includes/header.php');
include('includes/navigation.php');

?>

<h1>Request a quote</h1>

<?php echo $content; ?>

</div>

<?php

include('includes/rightColumn.php');
include('includes/footer.php');

?>